<?php
// +----------------------------------------------------------------------
// | SL 2018.8.17
// +----------------------------------------------------------------------
// | Copyright (c) 2018~2018 http://sl.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zq <1040657944@qq.com>
// +----------------------------------------------------------------------

namespace app\common\model;


use think\Model;

class ForumZanModel extends Model
{
    // 确定链接表名
    protected $name = 'forum_zan';
    //开启时间戳写入
    protected $autoWriteTimestamp = false;

    /**
     * 插入新的帖子
     */
    public function insertZan($param)
    {
        try{
            $result =  $this->allowField(true)->validate('ForumZanValidate')->save($param);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{
                return msg(1, '', '点赞成功!');
            }
        }catch(PDOException $e){

            return msg(-2, '', $e->getMessage());
        }
    }



    /**
     * 获取当前帖子点赞数据
     * @return mixed
     */
    public function getNoteZanList($where)
    {
        return $this->where($where)->column("comment_id");

    }

    /**
     * 验证是否已点赞
     * @return mixed
     */
    public function getZanInfo($params)
    {
        return $this->where(['comment_id'=>$params['comment_id'],'center_id'=>$params['center_id'],'note_id'=>$params['note_id']])->find();
    }

    /**
     * 删除点赞记录
     * @return mixed
     */
    public function delZanInfo($params)
    {
        return $this->where(['comment_id'=>$params['comment_id'],'center_id'=>$params['center_id'],'note_id'=>$params['note_id']])->delete();
    }

    /**
     * 计算comment列表的评论的点赞数
     * @return mixed
     */
    public function zanCount($note_id)
    {
//        return $this->where(['note_id'=>$note_id])->group('comment_id')->column('*,count(comment_id) comment_num','comment_id');
        $countList = $this->where(['note_id'=>$note_id])->group('comment_id')->field('*,count(comment_id) comment_num')->select();
        $tempCount = [];
        foreach($countList as $k=>$v){
            $tempCount[$v['comment_id']] = $v['comment_num'];
        }
        return $tempCount;
    }
}